<?php

namespace addons\litestore\controller\api;

use addons\litestore\model\Userwolk;
use addons\third\model\Third;
use app\common\controller\Api;
use app\common\library\Auth;
use EasyWeChat\Foundation\Application;
use think\Config;
use think\Db;

class Wolk extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        $this->model = new Userwolk();
    }

    public function getWolkdata()
    {
        $encryptedData = $this->request->post("encryptedData");
        $iv = $this->request->post("iv");

        $third = Third::where(['user_id' => $this->auth->id, 'platform' => 'wxapp'])->find();
        $sessionKey = $third['access_token'];

        $config = get_addon_config('litestore');
        $options = [
            //'debug'  => true,
            'mini_program' => [
                'app_id' => $config['AppID'],
                'secret' => $config['AppSecret'],
                'token' => 'component-token',
                'aes_key' => 'component-aes-key'
            ],
        ];
        $app = new Application($options);
        $this->miniProgram = $app->mini_program;
        $wolkData = $this->miniProgram->encryptor->decryptData($sessionKey, $iv, $encryptedData);
        #获取最后一条数据
        if (empty($wolkData['stepInfoList'])) {
            $this->error('未查询到数据，请在微信关注微信运动');
        }
        $num = count($wolkData['stepInfoList']);
        $nowWolknum = $wolkData['stepInfoList'][$num - 1]['step'];
        #更新用户步数
        $time = $wolkData['stepInfoList'][$num - 1]['timestamp'];
        #判断时间是否是今天
        $today = strtotime(date('Y-m-d'));
        if($time != $today){
            $this->error('未查询到今日数据，请在微信关注微信运动');
        }
        #查询是否存在
        $exsit = $this->model->where(['createtime'=>$time,'user_id'=>$this->auth->id])->find();
        if($exsit){
            #更新步数
            $exsit->save(['id'=>$exsit['id'],'user_id'=>$this->auth->id,'createtime'=>$time,'num'=>$nowWolknum]);
        }else{
            #新增步数
            $this->model->save(['user_id'=>$this->auth->id,'createtime'=>$time,'num'=>$nowWolknum]);
        }
        $this->success('已更新行走数据',['wolk_num'=>$nowWolknum,'score'=>$this->auth->score]);
    }
    #页面加载时触发请求 判断是否有行走步数已统计
    public function getWolkolddata(){
        #查询统计的步数
        $today = strtotime(date('Y-m-d'));
        $exsit = $this->model->where(['createtime'=>$today,'user_id'=>$this->auth->id])->find();
        if($exsit){
            $wolknum = $exsit['num'];
        }else{
            #新增步数
            $wolknum = "点击更新";
        }
        return $this->success('获取成功',['wolk_num'=>$wolknum,'score'=>$this->auth->score]);
    }
    public function edit()
    {
        $id = $this->request->request('id');
        $detail = $this->model->detail($this->user_id, $id);
        if ($detail->edit($this->request->post())) {
            return $this->success('成功');
        }
        return $this->error('更新失败');

    }
}